﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using Newtonsoft.Json;

namespace ZLCMS.Web.SystemManage.Basics
{
    public partial class TagList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _tagId;
        public string _userAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            
            _tagId = Request["uid"] == null ? "" : Request["uid"];
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            //初始化控件值

            if (!IsPostBack)
            {
               
            }
           
            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);

        }

        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopTagInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount)).AddOrder(Order.Asc("Sort"));
        }
        #endregion

        #region 查询数据总数 + protected int GetListCount()
        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopTagInfo>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
        #endregion

        public string GetStatusHtml(string status,string tid)
        {
            string statusHtml = "";
            if(status=="1")
            {
                statusHtml = "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=0'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认不启用标签'   >不启用</a>&nbsp;&nbsp;<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=9'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认冻结产品标签'   >冻结</a>&nbsp;&nbsp;";
            }
            else if(status=="0")
            {
                statusHtml = "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=1'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认启用标签'   >启用</a>&nbsp;&nbsp;<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=9'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认冻结产品标签'   >冻结</a>&nbsp;&nbsp;";
            }
            else if(status=="9")
            {
                statusHtml = "<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=1'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认启用标签'   >启用</a>&nbsp;&nbsp;<a href='/SystemManage/Basics/shop/taginfo/SetProductTag.aspx?uid=" + tid + "&status=9'  onclick='ZLAN_DWZ_EX.open(this);' target='ajaxTodo'    title='请确认不启用产品标签'   >不启用</a>&nbsp;&nbsp;";
            }
            return statusHtml;
        }
    }
}
